/*
 * Copyright (C) GSX Techedu Inc. All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 */

package com.nime.novel.crawl.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.ognl.ElementsAccessor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * @author liujialiang
 * @description Html富文本解析工具类
 * @team wuhan operational dev.
 * @date 2020/4/2 2:50 下午
 **/
public class HtmlParseUtil {

    // 默认白名单
    private static Whitelist whitelist = Whitelist.relaxed();

    // 富文本标签
    private static String[] tags = new String[]{"strong", "figure", "img", "h1", "i", "h2", "body",
        "h4", "head", "p", "s", "u", "ul", "html", "ol", "li", "span", "figcaption"};

    // 富文本属性
    private static String[] attrs = {"src", "alt", "style", "class"};

    private static String BODY_TAG = "body";

    // 加载白名单
    static {
        // 标签
        whitelist.addTags(tags);
        // 添加标签属性
        for (String tag : tags) {
            whitelist.addAttributes(tag, attrs);
        }
    }

    // 获取纯文本
    public static String getContent(String html) {
        if (StringUtils.isEmpty(html)) {
            return html;
        }
        Document doc = Jsoup.parse(html);
        return doc.text();
    }

    // 获取 纯文本+图片
    public static String getContentWithPic(String html) {
        if (StringUtils.isEmpty(html)) {
            return html;
        }
        Document doc = Jsoup.parse(html);
        Elements elements = doc.getElementsByClass("image");
        for (Element e : elements) {
            e.appendText("[图片]");
        }
        return doc.text();
    }

    // 白名单的富文本XSS后端过滤
    public static String safeHtml(String html) {
        if (StringUtils.isEmpty(html)) {
            return html;
        }
        //获得安全HTML，消除xss隐患
        Document dirty = Jsoup.parseBodyFragment(html.trim());
        Cleaner cleaner = new Cleaner(whitelist);
        Document clean = cleaner.clean(dirty);
        clean.outputSettings().prettyPrint(false);
        return clean.getElementsByTag(BODY_TAG).first().children().html();
    }

    public Elements parseList(String html, String css) {
        Document doc = Jsoup.parse(html);

        return doc.select("#hot_sale");

    }
}
